package com.imut.javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.imut.common.ConnectionFactory;
import com.imut.common.ResourceClose;



public class AdminDBAccess {	

	public Admin findAdminByLoginName(String loginName){
		Admin admin=null;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=ConnectionFactory.getConnection();
			String sql="select * from admin where loginName=?"; 
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, loginName);
			rs=pstmt.executeQuery();
			while(rs.next()){
				admin=new Admin();
				admin.setLoginName(rs.getString(1));
				admin.setName(rs.getString(2));
				admin.setPassword(rs.getString(3));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally{
			ResourceClose.close(rs, pstmt, conn);
		}
		return admin;
	}	

	public Admin login(String loginName,String password){
		Admin admin=null;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=ConnectionFactory.getConnection();
			String sql="select * from admin where loginName=? and password=?"; 
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, loginName);
			pstmt.setString(2, password);
			rs=pstmt.executeQuery();
			while(rs.next()){
				admin=new Admin();
				admin.setLoginName(rs.getString(1));
				admin.setName(rs.getString(2));
				admin.setPassword(rs.getString(3));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally{
			ResourceClose.close(rs, pstmt, conn);
		}
		return admin;
	}
}
